import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
import { MessageDto, EntityDtoOfInt64, MessageServiceProxy } from '@shared/service-proxies/service-proxies';
import { NotifyService } from '@abp/notify/notify.service';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { TokenAuthServiceProxy } from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { DataTable } from 'primeng/components/datatable/datatable';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { AppSessionService } from '@shared/common/session/app-session.service';

@Component({
  templateUrl: './messages.component.html',
  styleUrls: ['./messages.component.css'],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})
export class MessagesComponent extends AppComponentBase {

  @ViewChild('dataTable') dataTable: DataTable;
  @ViewChild('paginator') paginator: Paginator;


  //Filters
  advancedFiltersAreShown = false;
  selectedPermission = '';
  // role: number = undefined;
  appSession: AppSessionService;

  constructor(
    injector: Injector,
    private _http: Http,
    private _messagesServiceProxy: MessageServiceProxy,
    private _notifyService: NotifyService,
    private _fileDownloadService: FileDownloadService,
    private _tokenAuth: TokenAuthServiceProxy,
    private _activatedRoute: ActivatedRoute

  ) {
    super(injector);
    this.appSession = injector.get(AppSessionService);
  }

  getMessages(event?: LazyLoadEvent) {
    if (this.primengDatatableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengDatatableHelper.showLoadingIndicator();

    this._messagesServiceProxy.getAll(
      this.primengDatatableHelper.getSorting(this.dataTable),
      this.primengDatatableHelper.getMaxResultCount(this.paginator, event),
      this.primengDatatableHelper.getSkipCount(this.paginator, event)
    ).subscribe(result => {
      this.primengDatatableHelper.totalRecordsCount = result.totalCount;
      this.primengDatatableHelper.records = result.items;
      this.primengDatatableHelper.hideLoadingIndicator();
    });
  }

  // unlockUser(record): void {
  //     this._messagesServiceProxy.unlockUser(new EntityDtoOfInt64({ id: record.id })).subscribe(() => {
  //         this.notify.success(this.l('UnlockedTheUser', record.userName));
  //     });
  // }


  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }

  // exportToExcel(): void {
  //     this._userServiceProxy.getUsersToExcel()
  //         .subscribe(result => {
  //             this._fileDownloadService.downloadTempFile(result);
  //         });
  // }



  deleteMessages(messages: MessageDto): void {
    // if (messages.title === AppConsts.userManagement.defaultAdminUserName) {
    //     this.message.warn(this.l('{0}UserCannotBeDeleted', AppConsts.userManagement.defaultAdminUserName));
    //     return;
    // }

    this.message.confirm(
      '确认删除‘' +  messages.content + '’这条留言吗？',
      // this.l('UserDeleteWarningMessage', messages.title),
      (isConfirmed) => {
        if (isConfirmed) {
          this._messagesServiceProxy.delete(messages.id)
            .subscribe(() => {
              this.reloadPage();
              this.notify.success(this.l('SuccessfullyDeleted'));
            });
        }
      }
    );
  }
}
